package com.deeper.customview.ui.c4.exercise.c04e03;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * 绘制一张光盘，并让光盘不断旋转
 */
public class RotateCD extends View {

    private int centerX;
    private int centerY;
    private int radius1;
    private Bitmap arcBmp;
    private Canvas arcCanvas;
    private Paint arcPaint;
    private SweepGradient sweepGradient1;

    private static final int DARK_GRAY = 0xFF828282;
    private static final int COLOR_0 = 0xFFCE2100;
    private static final int COLOR_1 = 0xFFFAEE00;
    private static final int COLOR_2 = 0xFF2F85D6;
    private static final int COLOR_3 = 0xFF964A78;
    private static final int COLOR_4 = 0xFFFBF400;
    private static final int COLOR_5 = 0xFF318ECF;
    private static final int COLOR_6 = 0xFFE07800;
    private static final int COLOR_7 = 0xFF318ECF;
    private static final int COLOR_8 = 0xFFFBF400;
    private static final int COLOR_9 = 0xFF964A78;
    private static final int COLOR_10 = 0xFF2F85D6;
    private static final int COLOR_11 = 0xFFFAEE00;
    private static final int COLOR_12 = 0xFFCE2100;
    private SweepGradient sweepGradient2;
    private Matrix matrix;
    private int rotate;


    public RotateCD(Context context) {
        super(context);
    }


    public RotateCD(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }


    public RotateCD(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        int width = getMeasuredWidth();
        int height = getMeasuredHeight();

        int maxRadius = Math.min(width, height);
        centerX = maxRadius / 2;
        centerY = maxRadius / 2;
        radius1 = maxRadius / 2 - 100;

        arcBmp = Bitmap.createBitmap(radius1 * 2, radius1 * 2, Bitmap.Config.ARGB_8888);
        arcCanvas = new Canvas(arcBmp);
        arcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

        sweepGradient1 = new SweepGradient(radius1, radius1,
                new int[]{DARK_GRAY, Color.WHITE, DARK_GRAY, Color.WHITE, DARK_GRAY,
                        Color.WHITE, DARK_GRAY, Color.WHITE, DARK_GRAY}, null);

        sweepGradient2 = new SweepGradient(radius1, radius1,
                new int[]{COLOR_0, COLOR_1, COLOR_2, COLOR_3,
                        COLOR_4, COLOR_5, COLOR_6, COLOR_7,
                        COLOR_8, COLOR_9, COLOR_10, COLOR_11, COLOR_12}, null);

        rotate = 0;
        matrix = new Matrix();

        createBmp();
    }


    private void createBmp() {
        arcPaint.setShader(sweepGradient1);
        arcCanvas.drawCircle(radius1, radius1, radius1, arcPaint);

        arcPaint.setShader(sweepGradient2);
        arcCanvas.drawCircle(radius1, radius1, radius1 - 20, arcPaint);

        arcPaint.setShader(sweepGradient1);
        arcCanvas.drawCircle(radius1, radius1, 120, arcPaint);

        arcPaint.setShader(null);
        arcPaint.setColor(Color.BLACK);
        arcCanvas.drawCircle(radius1, radius1, 80, arcPaint);

        arcPaint.setColor(Color.WHITE);
        arcCanvas.drawCircle(radius1, radius1, 60, arcPaint);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        rotate += 3;
        if (rotate >= 360) {
            rotate = 0;
        }
        matrix.setRotate(rotate, centerX, centerY);
        canvas.setMatrix(matrix);
        invalidate();

        canvas.drawBitmap(arcBmp, 100, 100, null);
    }
}
